<?php

namespace App\Http\Controllers\Mini;

use App\Models\Ads;
use App\Models\Adsenses;
use App\Models\Areas;
use App\Models\Categorys;
use App\Models\Configs;
use App\Models\Helps;
use App\Models\Regions;
use App\Models\WordCategorys;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Trades;

use Illuminate\Support\Facades\Storage;

use App\Models\rewardTypes;

class CommonController extends Controller{
    /**
     * 获取平台分类
     * @route home.common.getCategorys
     * @param limit 显示条数
     */
    public function getCategorys(Request $request){
        try{
            $lists = Categorys::getCascaderList();//获取分类
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }
    
    public function getTxRate(Request $request){
    	try{
    		 $base = Configs::getConfig('base');
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $base['tx_rate']
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    
    //获取任务区域
    public function getArea(Request $request){
    	try{
    		//获取区域
            $area = Areas::getCascaderList();

    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $area
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }
    
    /**
     * 获取平台分类
     * @route home.common.getCategorys
     * @param limit 显示条数
     */
    public function getRewardTypes(Request $request){
    	try{
    		$lists = rewardTypes::getCascaderList();//获取分类
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $lists
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }

    //上传图片
    public function upImage(Request $request){
    	if (!empty($_FILES['file']['tmp_name'])) {
    		$disk = Storage::disk('image');
    		$photo = $request->file('file');
    		$realPath = $photo->getRealPath();//临时文件路径
    		$name = hash_file('md5',$realPath);
    		$time = date('Y-m-d');
    		$fileName = $time.'/'.$name.'.'.$photo->getClientOriginalExtension();
    		$bool = $disk->put($fileName, file_get_contents($realPath));
    		// 判断是否上传成功
    		if ($bool) {
    			return $this->writeJson(0,['path'=>$disk->url($fileName)]);
    		}
    	}
    	return $this->writeJson(1,null,'上传错误');
    }
    

    /**
     * 广告
     * @route home.common.advs
     * @param limit 显示条数
     * @param 广告位标识 $code
     * 30:web-首页banner广告位  
     * 31:web-首页中部广告位  
     * 32:web-供应商首页banner广告位  
     * 33:web_供应商详情底部广告位  
     * 34:web_供应商分类顶部广告位 
     * 35:web_活动分类顶部广告位
     * 36:web-活动报名成功底部广告位  
     * 37:web-商品分类顶部广告位  
     * 38:web-文档分类顶部广告位  
     * 39:web-新闻列表banner广告位
     * @return bool
     */
    public function advs(Request $request){
        try{
            $params = [
                'code' => (string)$request->post('code'),
                'status' => 1,
            ];
            $limit = (int)$request->post('limit');
            $where['name'] = $params['code'];
            $where['status'] = 1;
            $adsense_id = Adsenses::where($where)->value('id');
            $adsense_id = $adsense_id > 0 ? $adsense_id : 0;
            $data = [
                'adsense_id' => $adsense_id,
                'status' => 1
            ];
            $lists = Ads::getList($data, ['id','adsense_id','title','thumb','link'], $limit);
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }

    

    /**
     * 所有地区
     * @route home.common.regions
     */
    public function regions(Request $request){
        try{
            $lists = Regions::getCascaderList();
            return response()->json([
                "code" => 1,
                "msg" => "获取成功",
                "data" => $lists
            ]);
        }catch (\Exception $e){
            return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
        }
    }
    
    /**
     * 所有行业
     * @route home.common.regions
     */
    public function getTrades(Request $request){
    	try{
    	//	$lists = Regions::getCascaderList();
    		$lists = Trades::getList();
    		return response()->json([
    				"code" => 1,
    				"msg" => "获取成功",
    				"data" => $lists
    		]);
    	}catch (\Exception $e){
    		return response()->json(["code"=>0, "msg"=>$e->getMessage()]);
    	}
    }

}